package hot100.logos61To70;

import java.util.ArrayList;
import java.util.List;

/**
 * @author logos
 * date 2025/7/16 12:10
 * @version v1.0
 * @Package hot100.logos61To70
 */
public class logos61 {

    /**
     * 61. 分割回文串
     * https://leetcode.cn/problems/palindrome-partitioning/description/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution {
        public List<List<String>> partition(String s) {
            List<List<String>> ans = new ArrayList<>();
            dfs(s, 0, 0, new ArrayList<>(), ans);
            return ans;
        }

        private void dfs(String s, int i, int start, List<String> path, List<List<String>> ans) {
            if (i == s.length()) {
                ans.add(new ArrayList<>(path));
                return;
            }
            if (i < s.length() - 1) {
                dfs(s, i + 1, start, path, ans);
            }
            if (isFlag(s, start, i)) {
                path.add(s.substring(start, i + 1));
                dfs(s, i + 1, i + 1, path, ans);
                path.remove(path.size() - 1);
            }
        }

        private boolean isFlag(String s, int left, int right) {
            while (left <= right) {
                if (s.charAt(left++) != s.charAt(right--)) {
                    return false;
                }
            }
            return true;
        }
    }
}
